<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      let str = '3{a2{b2{c}}}' // abccbcc
      // {res:'',num:3} {res:'a',num:2}  c
      function fn() {
        const stack = []
        let num = 0
        let res = ''
        let result = ''
        for (let i = 0; i < str.length; i++) {
          if (!isNaN(+str[i])) {
            num = num * 10 + +str[i]
          } else if (str[i] == '{') {
            stack.push({ res, num })
            res = ''
            num = 0
          } else if (str[i] == '}') {
            let tail = stack.pop()
            let temp = res
            for (let i = 0; i < tail.num - 1; i++) {
              res += temp
            }
            res = tail.res + res
          } else {
            res += str[i]
          }
        }
        console.log(res)
      }
      fn(str)
    </script>
  </body>
</html>
